#if 0
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#define FIFO_SERVER "/tmp/fifoserver"
main(int argc,char** argv)
//参数为即将写入的字节数
{
    int fd;
    char w_buf[4096*2];
    int real_wnum;
    memset(w_buf,0,4096*2);
    if((mkfifo(FIFO_SERVER,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))
        printf("cannot create fifoserver\n");
    if(fd==-1)
        if(errno==ENXIO)
            printf("open error; no reading process\n");
        
    fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);
    //设置非阻塞标志
    //fd=open(FIFO_SERVER,O_WRONLY,0);
    //设置阻塞标志
    real_wnum=write(fd,w_buf,2048);
    if(real_wnum==-1)
    {
        if(errno==EAGAIN)
            printf("write to fifo error; try later\n");
    }
    else 
        printf("real write num is %d\n",real_wnum);
    real_wnum=write(fd,w_buf,5000);
    //5000用于测试写入字节大于4096时的非原子性
    //real_wnum=write(fd,w_buf,4096);
    //4096用于测试写入字节不大于4096时的原子性
    
    if(real_wnum==-1)
        if(errno==EAGAIN)
            printf("try later\n");
}
#endif
#include<stdio.h>  
#include<stdlib.h>  
#include <unistd.h>
#include<string.h>  
#include<fcntl.h>  
#include<sys/types.h>  
#include<sys/stat.h>  
int main(int argc,char *argv[])  
{  
    int fd;  
    char buf[20] = "hello world!!!\n";  
    if (access("my_fifo", F_OK) == -1) {
        if((mkfifo("my_fifo", O_CREAT|O_RDWR|0666)) < 0) {  
            perror("mkfifo");  
            exit(1);  
        }
    }
    if((fd = open("my_fifo", O_WRONLY)) < 0)  
    {  
        perror("open");  
        exit(1);  
    }  
    if((write(fd,buf,strlen(buf)-1)) < 0)  
    {  
        perror("write");  
        exit(1);  
    }  
    return 0;  
              
} 
 
